package action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import entitybean.l_sequence.EntityBeanL_Sequence;
import entitybean.oc_property.EntityBean_Oc_Property;
import entitybean.oc_property_tools.EntityBean_Oc_Property_Tools;
import form.PropertyForm;
import form.PropertyToolsForm;

import service.service_admin.Service_Admin;
import service.service_public.Service_Public;
import tools.InterfaceForResultSetToList;
import tools.PageBeanPlus;
import view.ViewOc_Property;
import view.ViewOc_Property_Tools;

public class Action_Oc_Property_Tools extends DispatchAction {
	private Service_Admin service_admin;
	private Service_Public service_public;

	public Service_Admin getService_admin() {
		return service_admin;
	}

	public void setService_admin(Service_Admin serviceAdmin) {
		service_admin = serviceAdmin;
	}

	public Service_Public getService_public() {
		return service_public;
	}

	public void setService_public(Service_Public servicePublic) {
		service_public = servicePublic;
	}

	public ActionForward admin_init_oc_property_tools(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		PropertyToolsForm propertytoolsform = (PropertyToolsForm) form;

		System.out.println("初始化海洋环境科考船");
		String sql = "select ID,PID,TOOLS_NAME,DEF,(SELECT OC_PROPERTY.PROPERTY_NAME FROM OC_PROPERTY WHERE ID=OC_PROPERTY_TOOLS.PID) PROPERTY_NAME from OC_PROPERTY_TOOLS where DEL='OFF'";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(propertytoolsform.getRowcountperpage()),
				Integer.valueOf(propertytoolsform.getCurrentpage()),
				"loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist = new InterfaceForResultSetToList();
		List<ViewOc_Property_Tools> list = interfaceforresultsettolist
				.ViewOc_Property_Tools(pagebeanplus.getFormatqueryresultset());
		request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
		request.setAttribute("list", list);
		request.setAttribute("propertytoolsform", propertytoolsform);
		System.out.println("list.size()" + list.size());

		return mapping.findForward("success");
	}

	public ActionForward admin_load_oc_property_tools(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		PropertyToolsForm propertytoolsform = (PropertyToolsForm) form;
		String id = propertytoolsform.getId();
		EntityBean_Oc_Property_Tools e = this.service_admin
				.Find_EntityBean_Oc_Property_Tools_by_Id(id);

		List<EntityBean_Oc_Property> list = (List<EntityBean_Oc_Property>) this.service_admin
				.Find_EntityBean_Oc_Property_by_property_kind(5);

		request.setAttribute("list", list);
		request.setAttribute("e", e);
		return mapping.findForward("success");
	}

	public ActionForward admin_add_oc_property_tools(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		PropertyToolsForm propertytoolsform = (PropertyToolsForm) form;
		List<EntityBean_Oc_Property> list = (List<EntityBean_Oc_Property>) this.service_admin
				.Find_EntityBean_Oc_Property_by_property_kind(5);

		request.setAttribute("list", list);
		return mapping.findForward("success");
	}

	public ActionForward admin_insert_oc_property_tools(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		PropertyToolsForm propertytoolsform = (PropertyToolsForm) form;
		String def = propertytoolsform.getDef();
		Integer pid = propertytoolsform.getPid();
		String toolsName = propertytoolsform.getTools_name();
		this.service_public.Find_L_Sequence_By_seq_Name("");

		EntityBeanL_Sequence l = this.service_public
				.Find_L_Sequence_By_seq_Name("SEQ_OC_PROPERTY_TOOLS");
		Integer id = l.getSeq_Value() + 1;
		l.setSeq_Value(id);
		EntityBean_Oc_Property_Tools e = new EntityBean_Oc_Property_Tools();
		e.setDef(def);
		e.setDel("OFF");
		e.setPid(pid);
		e.setTools_name(toolsName);
		e.setId(id);
		this.service_public.Update_L_Sequence(l);
		this.service_admin.Insert_Oc_Property_Tools(e);

		System.out.println("初始化海洋环境科考船");
		String sql = "select ID,PID,TOOLS_NAME,DEF,(SELECT OC_PROPERTY.PROPERTY_NAME FROM OC_PROPERTY WHERE ID=OC_PROPERTY_TOOLS.PID) PROPERTY_NAME from OC_PROPERTY_TOOLS where DEL='OFF'";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(propertytoolsform.getRowcountperpage()),
				Integer.valueOf(propertytoolsform.getCurrentpage()),
				"loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist = new InterfaceForResultSetToList();
		List<ViewOc_Property_Tools> list = interfaceforresultsettolist
				.ViewOc_Property_Tools(pagebeanplus.getFormatqueryresultset());
		request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
		request.setAttribute("list", list);
		request.setAttribute("propertytoolsform", propertytoolsform);
		System.out.println("list.size()" + list.size());

		return mapping.findForward("success");
	}

	public ActionForward admin_delete_oc_property_tools(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		PropertyToolsForm propertytoolsform = (PropertyToolsForm) form;
		String id=propertytoolsform.getId();
		id = this.cut_tail(id);
		this.service_admin.Delete_Oc_Property_Tools(id);
		System.out.println("初始化海洋环境科考船");
		String sql = "select ID,PID,TOOLS_NAME,DEF,(SELECT OC_PROPERTY.PROPERTY_NAME FROM OC_PROPERTY WHERE ID=OC_PROPERTY_TOOLS.PID) PROPERTY_NAME from OC_PROPERTY_TOOLS where DEL='OFF'";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(propertytoolsform.getRowcountperpage()),
				Integer.valueOf(propertytoolsform.getCurrentpage()),
				"loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist = new InterfaceForResultSetToList();
		List<ViewOc_Property_Tools> list = interfaceforresultsettolist
				.ViewOc_Property_Tools(pagebeanplus.getFormatqueryresultset());
		request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
		request.setAttribute("list", list);
		request.setAttribute("propertytoolsform", propertytoolsform);
		System.out.println("list.size()" + list.size());
		return mapping.findForward("success");
	}

	public ActionForward admin_update_oc_property_tools(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		PropertyToolsForm propertytoolsform = (PropertyToolsForm) form;
		String id=propertytoolsform.getId();
		String def = propertytoolsform.getDef();
		Integer pid = propertytoolsform.getPid();
		String toolsName = propertytoolsform.getTools_name();
		EntityBean_Oc_Property_Tools e = this.service_admin
		.Find_EntityBean_Oc_Property_Tools_by_Id(id);
		e.setDef(def);
		e.setPid(pid);
		e.setTools_name(toolsName);
		this.service_admin.Update_Oc_Property_Tools(e);
		
		
		System.out.println("初始化海洋环境科考船");
		String sql = "select ID,PID,TOOLS_NAME,DEF,(SELECT OC_PROPERTY.PROPERTY_NAME FROM OC_PROPERTY WHERE ID=OC_PROPERTY_TOOLS.PID) PROPERTY_NAME from OC_PROPERTY_TOOLS where DEL='OFF'";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(propertytoolsform.getRowcountperpage()),
				Integer.valueOf(propertytoolsform.getCurrentpage()),
				"loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist = new InterfaceForResultSetToList();
		List<ViewOc_Property_Tools> list = interfaceforresultsettolist
				.ViewOc_Property_Tools(pagebeanplus.getFormatqueryresultset());
		request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
		request.setAttribute("list", list);
		request.setAttribute("propertytoolsform", propertytoolsform);
		System.out.println("list.size()" + list.size());
		return mapping.findForward("success");
	}

	public ActionForward admin_search_oc_property_tools(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		PropertyToolsForm propertytoolsform = (PropertyToolsForm) form;

		System.out.println("初始化海洋环境科考船");
		String sql = "select ID,PID,TOOLS_NAME,DEF,(SELECT OC_PROPERTY.PROPERTY_NAME FROM OC_PROPERTY WHERE ID=OC_PROPERTY_TOOLS.PID) PROPERTY_NAME from OC_PROPERTY_TOOLS where DEL='OFF'";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(propertytoolsform.getRowcountperpage()),
				Integer.valueOf(propertytoolsform.getCurrentpage()),
				"loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist = new InterfaceForResultSetToList();
		List<ViewOc_Property_Tools> list = interfaceforresultsettolist
				.ViewOc_Property_Tools(pagebeanplus.getFormatqueryresultset());
		request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
		request.setAttribute("list", list);
		request.setAttribute("propertytoolsform", propertytoolsform);
		System.out.println("list.size()" + list.size());

		return mapping.findForward("success");
	}

	private String cut_tail(String str) {
		String cut = str;
		if (str.endsWith(",")) {
			cut = cut.substring(0, cut.length() - 1);
		}
		return cut;
	}
}
